/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.sample.helloworld.console;

import nemos.launch.progress.IProgressListener;
import nemos.launch.progress.IRecords;
import nemos.launch.progress.IStatus;

import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

/**
 * HelloWorld : The Hello World class.
 * 
 * <p>
 * This class waits for the end of the launch progress and displays the
 * well-known message 'Hello World!' to the standard output.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class HelloWorld implements IProgressListener {

    /** The bundle context */
    private final BundleContext mContext;

    /**
     * Constructor
     * 
     * @param pContext
     *            The bundle context
     */
    public HelloWorld(final BundleContext pContext) {
        super();

        mContext = pContext;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.progress.IProgressListener#init(nemos.launch.progress.IRecords,
     *      int)
     */
    @Override
    public void init(@SuppressWarnings("unused") final IRecords pRecords,
            @SuppressWarnings("unused") final int pTargetLevel) {
        // Nothing to do : waiting for the end
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.progress.IProgressListener#progressChanged(nemos.launch.progress.IStatus)
     */
    @Override
    public void progressChanged(@SuppressWarnings("unused") final IStatus pStatus) {
        // Nothing to do : waiting for the end
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.progress.IProgressListener#ended()
     */
    @Override
    public void ended() {
        System.out.println("Hello World!"); //$NON-NLS-1$

        // Stop framework
        try {
            mContext.getBundle(0).stop();
        } catch (final BundleException e) {
            System.err.println("Unable to stop framework."); //$NON-NLS-1$
            e.printStackTrace();
        }
    }
}
